package com.lemt.user.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.lemt.common.result.Res;
import com.lemt.user.entity.User;
import com.lemt.user.entity.dto.UserLoginDto;
import com.lemt.user.entity.vo.UserInfoVo;
import com.lemt.user.entity.vo.UserRegisterVo;
import com.lemt.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zik
 * @since 2023-09-10
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户权限认定")
public class UserController {
    @Resource(name = "IUserService")
    private IUserService userService;

    @ApiOperation("获取所有用户")
    @GetMapping("/admin/list")
    public Res<List<User>> getUser() {
        return Res.ok(userService.list());
    }

//    @ApiOperation(value = "添加用户")
//    @ApiOperationSupport(ignoreParameters = {"user.id","user.createTime","user.updateTime","user.status","user.isDelete"},includeParameters = {"user.username","user.password","user.phone"})
//    @PostMapping("/admin/saveUser")
//    public Res<Boolean> addUser(@RequestBody User user){
//        return Res.ok(userService.save(user));
//    }

    @ApiOperation(value = "删除user")
    @ApiOperationSupport(includeParameters = {"user.id"})
    @DeleteMapping("/admin/removeUserById")
    public Res<Boolean> remove(@RequestBody User user) {
        return Res.ok(userService.removeById(user));
    }

    @ApiOperation(value = "修改用户信息")
    @ApiOperationSupport(ignoreParameters = {"user.id","user.createTime","user.updateTime","user.status","user.isDelete"})
    @PostMapping("/admin/updateUserById")
    public  Res<Boolean> update(@RequestBody User user){
        return Res.ok(userService.updateById(user));
    }


    @ApiOperation(value = "用户注册")
    @PostMapping("/register")
    public Res<String> register(@RequestBody UserRegisterVo userRegisterVo) {
        String register = userService.register(userRegisterVo);
        if (register.equals("注册成功")){
            return Res.ok(register);
        }
        return Res.fail(register);
    }

    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public Res<String> login(@RequestBody UserLoginDto userLoginDto) {
        String login = userService.login(userLoginDto);
        if (login == null){
            return Res.fail("登录失败");
        }
        return Res.ok(login);

    }

    @ApiOperation("获取用户")
    @GetMapping("/getUserInfo/{id}")
    public Res<UserInfoVo> getInfo(@PathVariable Integer id){
        return Res.ok(userService.getUserInfo(id));
    }

}
